package gdca.webservice.common;

import gdca.util.CAManager;

import java.net.MalformedURLException;

public class GdcaOperate {

	public ResponseInfo dealGdcaServise(RequestInfo requestInfo) throws Exception {
		String customCode = "0011"; 
		String operData =null; 
		String isSign = "1"; 
		String isEnc = "0";	
		//生成请求原文XML字符串
		operData = genOperDate(requestInfo);
		System.out.println("原文请求为:\n" + operData);
		String signValue = new CAManager().sign(operData, 0); 
		String response = gdcaBusyDealCa(customCode, operData, isSign, signValue, isEnc);
		String[] responses = response.split("\\|\\|");
		String gdcaIsSign = responses[0];
		String gdcaIsEnc = responses[1];		                            
		String gdcaSignValue = responses[2];
		String gdcaStrRespnseXml = responses[3];		  
		ResponseInfo responseInfo = genResponseInfo(gdcaStrRespnseXml);
		
		return responseInfo;
	}
	
	public ResponseInfo genResponseInfo(String resXml) throws Exception {
		XmlParse xmlParse = new XmlParse(resXml, "UTF-8");
		String acceptCode = null;
		String isSuccess = null;
		String errorCode = null;
		String errorValue = null;
		String trustId = null;
		String keyId = null;
		String signCert = null;
		String encCert = null;
		String cryptPrivKey = null;
		String encSn = null;
		String signSn = null;
		String caCert = null;
		String initPassword = null;
		XMLData temp = xmlParse.getOnlyNodeByXPath("/responseInfo/acceptCode");
		if(temp!=null)
		{
			acceptCode = temp.getValue();
		}
		
		temp = xmlParse.getOnlyNodeByXPath("/responseInfo/isSuccess");
		if(temp!=null)
		{
			isSuccess = temp.getValue();
		}
		
		temp = xmlParse.getOnlyNodeByXPath("/responseInfo/errorCode");
		if(temp!=null)
		{
			errorCode = temp.getValue();
		}
		
		temp = xmlParse.getOnlyNodeByXPath("/responseInfo/errorValue");
		if(temp!=null)
		{
			errorValue = temp.getValue();
		}
		
		temp = xmlParse.getOnlyNodeByXPath("/responseInfo/trustId");
		if(temp!=null)
		{
			trustId = temp.getValue();
		}
		
		temp = xmlParse.getOnlyNodeByXPath("/responseInfo/keyId");
		if(temp!=null)
		{
			keyId = temp.getValue();
		}
		
		temp = xmlParse.getOnlyNodeByXPath("/responseInfo/signCert");
		if(temp!=null)
		{
			signCert = temp.getValue();
		}
		
		temp = xmlParse.getOnlyNodeByXPath("/responseInfo/encCert");
		if(temp!=null)
		{
			encCert = temp.getValue();
		}
		
		temp = xmlParse.getOnlyNodeByXPath("/responseInfo/cryptPrivKey");
		if(temp!=null)
		{
			cryptPrivKey = temp.getValue();
		}
		
		temp = xmlParse.getOnlyNodeByXPath("/responseInfo/encSn");
		if(temp!=null)
		{
			encSn = temp.getValue();
		}
		
		temp = xmlParse.getOnlyNodeByXPath("/responseInfo/signSn");
		if(temp!=null)
		{
			signSn = temp.getValue();
		}		
		
		temp = xmlParse.getOnlyNodeByXPath("/responseInfo/caCert");
		if(temp!=null)
		{
			caCert = temp.getValue();
		}	
		
		temp = xmlParse.getOnlyNodeByXPath("/responseInfo/initPassword");
		if(temp!=null)
		{
			initPassword = temp.getValue();
		}
		
		ResponseInfo responseInfo = new ResponseInfo();
		responseInfo.setAcceptCode(acceptCode);
		//responseInfo.setBuzzId(buzzId);
		responseInfo.setCryptPrivKey(cryptPrivKey);
		responseInfo.setEncCert(encCert);
		responseInfo.setErrorCode(errorCode);
		responseInfo.setErrorValue(errorValue);
		responseInfo.setInitPassword(initPassword);
		responseInfo.setIsSuccess(isSuccess);
		responseInfo.setKeyId(keyId);
		//responseInfo.setMediumCode(mediumCode);
		//responseInfo.setP7Cert(cert);
		responseInfo.setSignCert(signCert);
		responseInfo.setTrustId(trustId);
		//responseInfo.setUserPassword(userPassword);
		responseInfo.setSignSn(signSn);		
		responseInfo.setEncSn(encSn);
		responseInfo.setCaCert(caCert);
		return responseInfo;
	}

	public String gdcaBusyDealCa(String customCode, String operData, String isSign, String signValue, String isEnc) throws Exception
	{
		java.net.URL url = new java.net.URL(GdcaProperties.getProperties("url"));
		GdcaCommonServiceServiceLocator locator = new GdcaCommonServiceServiceLocator();
		GdcaCommonServiceSoapBindingStub stub = new GdcaCommonServiceSoapBindingStub(url, locator);
		stub.setUsername(GdcaProperties.getProperties("user"));
		stub.setPassword(GdcaProperties.getProperties("password"));
		String res = stub.gdcaBusyDealCa(customCode, operData, isSign, signValue, isEnc);
		System.out.println("GDCA返回值为：\n" + res);
		return res;
	}
	
	public String genOperDate(RequestInfo requestInfo)
	{ 
		String operData = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
		//业务信息
		operData = operData + "";
		operData = operData + "<requestInfo>";
		operData = operData + "<busyInfoRequest>";
		operData = operData + "<customCode>" + requestInfo.getCustomeCode() + "</customCode>";
		operData = operData + "<busyType>" + requestInfo.getBusyType() + "</busyType>";
		operData = operData + "<certType>" + requestInfo.getCertType() + "</certType>";
		operData = operData + "<isWbNew>" + "true" + "</isWbNew>";
		operData = operData + "<isConnectCa>" + "true" + "</isConnectCa>";
		operData = operData + "</busyInfoRequest>";
		
		//证书信息
		operData = operData + "<certInfoRequest>";
		operData = operData + "<trustId>" + requestInfo.getTrustId() + "</trustId>";
		operData = operData + "<password>" + requestInfo.getPassword() + "</password>";
		operData = operData + "<initPassword>" + requestInfo.getInitPassword() + "</initPassword>";
		operData = operData + "<cn>" + requestInfo.getCn() + "</cn>";
		operData = operData + "<o>" + requestInfo.getO() + "</o>";
		operData = operData + "<ous>";
		operData = operData + "<ou>" + requestInfo.getOu() + "</ou>";
		operData = operData + "</ous>";
		operData = operData + "<c>" + requestInfo.getC() + "</c>";
		operData = operData + "<s>" + requestInfo.getS() + "</s>";
		operData = operData + "<l>" + requestInfo.getL() + "</l>";
		operData = operData + "<e>" + requestInfo.getE() + "</e>";
		operData = operData + "<publicKey>" + requestInfo.getPublicKey() + "</publicKey>";
		operData = operData + "</certInfoRequest>";
		
		//证书扩展信息
		operData = operData + "<certExtraInfos>";
		operData = operData + "<certExtraInfo>";
		operData = operData + "<name>" + "" + "</name>";
		operData = operData + "<value>" + "" + "</value>";
		operData = operData + "</certExtraInfo>";
		
		operData = operData + "<certExtraInfo>";
		operData = operData + "<name>" + "" + "</name>";
		operData = operData + "<value>" + "" + "</value>";
		operData = operData + "</certExtraInfo>";
		operData = operData + "</certExtraInfos>";
		
		//应用信息
		operData = operData + "<appExtraInfos>";
		operData = operData + "<appExtraInfo>";
		operData = operData + "<name>" + "纳税人登记号" + "</name>";
		operData = operData + "<value>" + requestInfo.getNsrdjh() + "</value>";
		operData = operData + "</appExtraInfo>";
		
		operData = operData + "<appExtraInfo>";
		operData = operData + "<name>" + "子税号" + "</name>";
		operData = operData + "<value>" + requestInfo.getZsh() + "</value>";
		operData = operData + "</appExtraInfo>";
		operData = operData + "</appExtraInfos>";
		
		operData = operData + "</requestInfo>";
		return operData;
	}
}
